eval 'exec perl -wS $0 ${1+"$@"}'
    if 0;
#########################
#                       #
#   Globale Variablen   #
#                       #
#########################

%dir_content = ();
$start_dir = '/install/linux_from_scratch';
$string_to_replace = 'charset=windows-1251';
$string_needed = 'charset=koi8-r';
### main ###

&get_dir_hash($start_dir, \%dir_content);
&replace_strings(\%dir_content);

### procedures ###

sub get_dir_hash {
	my ($prj_dir, @prj_dir_list, $dirs_hash_ref, $entry);
	$prj_dir = shift;
	$dirs_hash_ref = shift;
	if (opendir(COPY_FROM, $prj_dir)) {
		@prj_dir_list = readdir(COPY_FROM);
		closedir (COPY_FROM);
		foreach $entry (@prj_dir_list) {
			$entry =~ /^\.$/ and next;
			$entry =~ /^\.\.$/ and next;
            $file = $prj_dir.'/'.$entry;
            &get_dir_hash($file, $dirs_hash_ref) if (-d $file);
            next if (!-f $file);
			$$dirs_hash_ref{$file}++;
            print $file."\n";
		};
	};
};

sub replace_strings {
	my $dirs_hash_ref = shift;
    foreach my $file (keys %$dirs_hash_ref) {
        open ENTRY, $file;
        my @strings = <ENTRY>;
        close ENTRY;
        my @new_strings = ();
        print "$file\n";
        foreach my $string (@strings) {
            push(@new_strings, $string) and next if ($string !~ /$string_to_replace/o);
#            print "$string\n";
            $string =~ s/$string_to_replace/$string_needed/og;
#            print "$string\n";
            push(@new_strings, $string);
        };
#print "@new_strings" and die;
#unlink $file;
        open (ENTRY, ">$file");
        foreach my $string (@new_strings) {
            print ENTRY $string;
        };
        close ENTRY;
    };
};
